
$(function() {
	$("input[id^='action_s']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'info.php?do=Priv.ajax_action',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label: item.title + " [" + item.cc + ", " + item.action + "]",
							value: item.action
						}
					}));
				}
			});
		},
		
		minLength: 1
	});
});

var Priv = {
	dialog_add_: function(url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_add")[0];
	
				var body = { };
				body.name = form.name.value;
				body.cc = form.cc.value;
				body.action_ = form.action_.value;
				body.content = form.content.value;
	
				body.status = form.status.value;
	
				$(this).dialog("close");
	
				var url = 'info.php?do=Priv.addPost';
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	popup: function(e, p2, id, status) {
		$.cookie('id_', id);

		$.cookie('status_', status);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'info.php?do=Priv.edit&id=' + id;	
		var c2 = 'info.php?do=Priv.view&id=' + id;
		var c3 = 'info.php?do=Priv.copy&id=' + id;
		
		var c4 = 'info.php?do=Priv.emp&id=' + id;
		var c5 = 'info.php?do=Priv.role&id=' + id;

		var a1 = ['编辑信息', 'url', 'Priv.dialog_edit_', '550x320'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '550x320'];
		var a3 = ['复制信息', 'url', 'Priv.dialog_copy_', '550x320'];
		
		var a4 = ['分配用户', 'url', 'Priv.dialog_emp_', '650x500'];
		var a5 = ['分配角色', 'url', 'Priv.dialog_role_', '650x500'];
		
		var s = c1 + ", " + c2 + ", " + c3 + ", " + c4 + ", " + c5;

		return popup(e, [a1, a2, a3, a4, a5], s);
	},
	
	dialog_edit_: function(url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},
			
			"修改": function() {			
				var form = $("#form_edit")[0];

				var body = { };
				body.id = form.id.value;
				body.name = form.name.value;
				body.cc = form.cc.value;
				body.action_ = form.action_.value;
				body.content = form.content.value;
				body.orderby = form.orderby.value;
				body.status = form.status.value;
				
				$(this).dialog("close");

				var url = 'info.php?do=Priv.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}		
		});

		dialog_edit(url, title, xy);
	},
	
	dialog_copy_: function(url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},
			
			"复制": function() {			
				var form = $("#form_edit")[0];

				var body = { };
				body.name = form.name.value;
				body.cc = form.cc.value;
				body.action_ = form.action_.value;
				body.content = form.content.value;
				body.status = form.status.value;
				
				$(this).dialog("close");

				var url = 'info.php?do=Priv.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}		
		});

		dialog_edit(url, title, xy);
	},
	
	dialog_emp_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var foo = [];
				
				$("#emp option").each(function(i, n) {
					foo[i] = $(n).val();
				});
				
				var form = $("#form_edit")[0];

				var body = { };
				body.id = form.id.value;
				body.list = foo.join(",");

				$(this).dialog("close");

				var url = 'info.php?do=Priv.empPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
					// alert("更新成功！");
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	dialog_role_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var foo = [];
				
				$("#role option").each(function(i, n) {
					foo[i] = $(n).val();
				});
				
				var form = $("#form_edit")[0];

				var body = { };
				body.id = form.id.value;
				body.list = foo.join(",");

				$(this).dialog("close");

				var url = 'info.php?do=Priv.rolePost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
					// alert("更新成功！");
				});
			}
		});

		dialog_edit(url, title, xy);
	}
};
